package cn.xxstudy.visitshop.network.repository

import cn.xxstudy.visitshop.network.data.ResponseData
import cn.xxstudy.visitshop.network.data.Response
import java.lang.Exception

/**
 * @author ：DELL on 2021/3/23 .
 * @packages ：cn.xxstudy.visitshop.network.repository .
 * TODO:公共仓库请求处理
 */
open class BaseRepository {

    suspend fun <T : Any> request(call: suspend () -> ResponseData<T>): Response<T> {
        return try {
            val request = call.invoke()
            if (request.code == 0) {
                Response.SUCCESS(request.data)
            } else {
                Response.FAILED(request.message)
            }
        } catch (e: Exception) {
            Response.FAILED(e.message ?: "异常")
        }
    }
}